import {
  getArticleDetail, getArticleList, gitArticleNavList,
  getArticleSwipers, getArticleFe, getArticleBe,
  getArticleReading, getArticleLinux, getArticleLeetcode, getArticleNews, getArticleJava,
  getAlipay
} from '@/services';
import { IArticleDetail, IArticleItems, IArticleNavList, IArticleNavFe, IArticleSwiper } from '@/types';
import { Effect, Reducer, history } from 'umi';
export interface ArticleModelState {
  articleList: IArticleItems[];
  articleLength: number;
  articleDetail: Partial<IArticleDetail>; // 把接口的属性变成可选的
  getArticleNavList: IArticleNavList[];
  getArticleFe: IArticleItems[]
  articleSwiper: IArticleSwiper[];
}

export interface ArticleModelType {
  namespace: 'article';
  state: ArticleModelState;
  effects: {
    getArticleList: Effect;
    getArticleDetail: Effect;
    getArticleNavList: Effect;
    getArticleFe: Effect;
    getArticleBe: Effect;
    getArticleReading: Effect;
    getArticleLinux: Effect;
    getArticleLeetcode: Effect;
    getArticleNews: Effect;
    getArticleJava: Effect;
    getArticleSwipers: Effect;
    getAlipay: Effect;
  };
  reducers: {
    save: Reducer<ArticleModelState>;
  };
}

const ArticleModel: ArticleModelType = {
  // 数据模型，用来隔离数据防止相互影响
  namespace: 'article',

  // 数据模型状态，相当于vuex的state
  state: {
    articleList: [],
    articleLength: 0,
    // articleDetail: {} as IArticleDetail // 强制类型推断
    articleDetail: {},
    getArticleNavList: [],
    getArticleFe: [],
    articleSwiper: []
  },

  // 异步操作，相当于vuex的action
  effects: {
    // *相当于async，yield相当于await
    *getArticleList({ payload }, { call, put, select }) {
      let { articleList } = yield select((state: { article: ArticleModelState }) => state.article);
      let result = yield call(getArticleList, payload);
      if (result.data) {
        let list = result.data[0];
        if (payload !== 1) {
          list = [...articleList, ...list];
        }
        yield put({
          type: 'save',
          payload: {
            articleList: list,
            articleLength: result.data[1]
          }
        })
      }
    },
    *getArticleDetail({ payload }, { call, put }) {
      let result = yield call(getArticleDetail, payload);
      localStorage.setItem('Floor', result.data.toc)

      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            articleDetail: result.data
          }
        })
      }
    },

    *getArticleNavList({ payload }, { call, put }) {
      let result = yield call(gitArticleNavList, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            getArticleNavList: result.data,
          }
        })
      }
    },
    // nav--前端数据
    *getArticleFe({ payload }, { call, put }) {
      console.log(payload);

      let result = yield getArticleFe(payload)
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            getArticleFe: result.data[0],
          }
        })
      }
    },
    // nav--后端数据
    *getArticleBe({ payload }, { call, put }) {
      let result = yield call(getArticleBe, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            getArticleFe: result.data[0],
          }
        })
      }
    },
    // nav--阅读数据
    *getArticleReading({ payload }, { call, put }) {
      let result = yield call(getArticleReading, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            getArticleFe: result.data[0],
          }
        })
      }
    },
    // nav--Linux数据
    *getArticleLinux({ payload }, { call, put }) {
      let result = yield call(getArticleLinux, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            getArticleFe: result.data[0],
          }
        })
      }
    },
    // nav--Leetcode数据
    *getArticleLeetcode({ payload }, { call, put }) {
      let result = yield call(getArticleLeetcode, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            getArticleFe: result.data[0],
          }
        })
      }
    },
    // nav--News数据
    *getArticleNews({ payload }, { call, put }) {
      let result = yield call(getArticleNews, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            getArticleFe: result.data[0],
          }
        })
      }
    },
    // nav--Java数据
    *getArticleJava({ payload }, { call, put }) {
      let result = yield call(getArticleJava, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            getArticleFe: result.data[0],
          }
        })
      }
    },
    //轮播
    *getArticleSwipers({ payload }, { call, put }) {
      let result = yield call(getArticleSwipers, payload);

      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            articleSwiper: result.data
          }
        })
      }
    },
    *getAlipay({ payload }, { call, put }) {
      let result = yield call(getAlipay, payload);
      // console.log(result, '支付宝----------------');
      if (result.statusCode === 201) {
        window.location.href = result.data
        yield put({
          type: 'save',
        })
      }
    }

  },


  // 同步操作，相当于vuex的 mutation
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

  }
};

export default ArticleModel;
